package cn.uniquesoft.controller;

import cn.uniquesoft.controller.base.BaseController;
import cn.uniquesoft.manager.ClientManager;
import cn.uniquesoft.page.DataGrid;
import cn.uniquesoft.service.IConsultantService;
import cn.uniquesoft.service.ISearchService;
import cn.uniquesoft.util.ContextUtil;
import cn.uniquesoft.util.DateUtil;
import cn.uniquesoft.util.FileUpUtil;
import cn.uniquesoft.vo.AnswerVO;
import cn.uniquesoft.vo.ConsultantVO;
import cn.uniquesoft.vo.SearchVO;
import cn.uniquesoft.vo.UserVO;
import cn.uniquesoft.vo.base.BaseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;
@Controller
@RequestMapping(value="/consultant")
public class ConsultantController extends BaseController<ConsultantVO> {
 private IConsultantService consultantService;
	@Autowired
	private ISearchService searchService;
	private File path;

	public ConsultantController(IConsultantService consultantService){
	 super();
	 this.consultantService = consultantService;
		this.baseService =consultantService;
		this.indexName = "consultant/index";
 }

 @RequestMapping(value = "/save")
	@ResponseBody
	public AnswerVO save(ConsultantVO item) {
		AnswerVO answerVO = new AnswerVO();
		try {
			MultipartFile file = item.getUpfile();
			if(file.getSize()>0){
				String tomurl = ContextUtil.getRequest().getSession().getServletContext().getRealPath("/");

				// 获得上传文件原始名
				String oldname = file.getOriginalFilename();
				// 算出后缀名
				String ext = oldname.substring(oldname.lastIndexOf("."));
				// 文件名为：问题名称 + image + UUID + 后缀
				String fileName = "consultant" + DateUtil.date2timestamp(new Date(), DateUtil.FORMAT_DATETIME) + ext;
				String filepath = "files"+ File.separator + "consultant" + File.separator;
				String fileFolder = tomurl+filepath;
				File upload = new File(fileFolder);
				if(!upload.exists()) upload.mkdirs();
				File localFile = new File(fileFolder, fileName);
				
				// 直接写入到后台服务器，简单且快
				file.transferTo(localFile);
				item.setCpicname(oldname);
				item.setCpicpath(fileName);
				item.setCpicsize(FileUpUtil.convertFileSize(file.getSize()));
			}
			this.consultantService.save(item);
			int itemId = ((BaseVO) item).getIid();
			if (itemId > 0) {
				SearchVO searchVO = new SearchVO();
				searchVO.setTname("consultant");
				searchVO.setTid(item.getIid()+"");
				searchVO.setTvalue(item.toString());
				searchVO.setTtitle(item.getCname());
				System.out.println(item.toString());
				searchVO.setId(searchVO.getTid() + "_" + searchVO.getTname());
				this.searchService.save(searchVO);
				answerVO.setSuccess(true);
				answerVO.setMsg("新增成功!");
			} else {
				answerVO.setSuccess(false);
				answerVO.setMsg("新增失败!");
			}
			answerVO.setData(itemId);
		} catch (Exception e) {
			e.printStackTrace();
			answerVO.setSuccess(false);
			answerVO.setMsg("新增失败," + e.getMessage());
		}
		return answerVO;
	}
  @RequestMapping(value = "/edit")
	@ResponseBody
	public AnswerVO edit(ConsultantVO item) {
		AnswerVO answerVO = new AnswerVO();
		try {
			MultipartFile file = item.getUpfile();
			if(file.getSize()>0){
				String tomurl = ContextUtil.getRequest().getSession().getServletContext().getRealPath("/");
				// 获得上传文件原始名
				String oldname = file.getOriginalFilename();
				// 算出后缀名
				String ext = oldname.substring(oldname.lastIndexOf("."));
				// 文件名为：问题名称 + image + UUID + 后缀
				String fileName = "consultant" + DateUtil.date2timestamp(new Date(), DateUtil.FORMAT_DATETIME) + ext;
				String filepath = "files"+ File.separator + "consultant" + File.separator;
				String fileFolder = tomurl+filepath;
				File upload = new File(fileFolder);
				if(!upload.exists()) upload.mkdirs();
				File localFile = new File(fileFolder, fileName);
				// 直接写入到后台服务器，简单且快
				file.transferTo(localFile);
				item.setCpicname(oldname);
				item.setCpicpath(fileName);
				item.setCpicsize(FileUpUtil.convertFileSize(file.getSize()));
			}
			int result = this.consultantService.edit(item);
			if (result > 0) {
				this.searchService.remove(item.getIid()+"", "company");
				SearchVO searchVO = new SearchVO();
				searchVO.setTname("consultant");
				searchVO.setTid(item.getIid()+"");
				searchVO.setTtitle(item.getCname());
				searchVO.setTvalue(item.toString());
				System.out.println(item.toString());
				searchVO.setId(searchVO.getTid() + "_" + searchVO.getTname());
				this.searchService.save(searchVO);
				answerVO.setSuccess(true);
				answerVO.setMsg("修改成功!");
			} else {
				answerVO.setSuccess(false);
				answerVO.setMsg("修改失败!");
			}
			answerVO.setData(result);
		} catch (Exception e) {
			answerVO.setSuccess(false);
			answerVO.setMsg("修改失败," + e.getMessage());
		}

		return answerVO;
	}


	@RequestMapping(value = "/removeById")
	@ResponseBody
	@Override
	public AnswerVO removeById(int  iid) {
		AnswerVO answerVO = new AnswerVO();
		try {
			int result = this.baseService.removeById(iid);

			if (result > 0) {
				this.searchService.remove(iid+"", "consultant");
				answerVO.setSuccess(true);
				answerVO.setMsg("删除成功!");
			} else {
				answerVO.setSuccess(false);
				answerVO.setMsg("删除失败!");
			}
			answerVO.setData(result);
		} catch (Exception e) {
			answerVO.setSuccess(false);
			answerVO.setMsg("删除失败," + e.getMessage());
		}

		return answerVO;
	}

	@RequestMapping(value = "/removeByIds")
	@ResponseBody
	@Override
	public AnswerVO removeByIds(String ids) {
		AnswerVO answerVO = new AnswerVO();
		try {
			int result = this.baseService.removeByIds(ids);

			if (result > 0) {
				String[] ss = ids.split(",");
				for(int i=0;i<ss.length;i++){
					this.searchService.remove(ss[i]+"", "consultant");
				}
				answerVO.setSuccess(true);
				answerVO.setMsg("删除成功!");
			} else {
				answerVO.setSuccess(false);
				answerVO.setMsg("删除失败!");
			}
			answerVO.setData(result);
		} catch (Exception e) {
			answerVO.setSuccess(false);
			answerVO.setMsg("删除失败," + e.getMessage());
		}

		return answerVO;
	}

	@RequestMapping(value = "/pageQuery")
	@ResponseBody
	public DataGrid pageQuery(ConsultantVO item) {
		DataGrid dg = new DataGrid();
		UserVO userVO = null;
		try {
			userVO = ClientManager.getInstance().getClient().getUser();
		} catch (Exception e) {

		}
		if (userVO != null) {
			if (userVO.getIvisible() == 1) {

			} else if (userVO.getIvisible() == 2) {
				item.setIbank(userVO.getIorgname());
			} else {
				item.setIcreator(userVO.getIid());
			}
		}
		return this.baseService.pageQuery(item);
	}
}
